package top.smallway.lightbandassistant_app.network;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.blankj.utilcode.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import top.smallway.lightbandassistant_app.data.Keys;

public class Okhttp {
    private static final OkHttpClient httpClient = new OkHttpClient();


    /**
     * 该方法为获取最新版本的版本号及下载地址，可用JSONObject包装
     *
     * @return String
     * @throws IOException
     */
    public static String update() throws IOException {
        Request request = new Request.Builder().url("https://bs.smallway.top/update2").build();

        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static boolean upload(String hash) throws IOException {
        Request request = new Request.Builder().url("https://bs.smallway.top/upload?hash=" + hash).build();

        Call call = httpClient.newCall(request);
        Response response = call.execute();
        String static_ = response.body().string();
        return !static_.equals("0");
    }

    public static String uploadlist(String name, String hash) throws IOException {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/uploadlist").newBuilder();
        urlBuilder.addQueryParameter("name", name);
        urlBuilder.addQueryParameter("hash", hash);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();

        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();

    }

    public static String getcode() throws IOException {
        final String url = "https://springboot-web-frawork-ytx-crfngcilis.cn-hangzhou.fcapp.run/getcode";
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static String dellist(String name) throws IOException {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/dellist").newBuilder();
        urlBuilder.addQueryParameter("name", name);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static String getlist() throws IOException {
        Request request = new Request.Builder().url("https://bs.smallway.top/getlist").build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static boolean downloadlist(String hash) throws IOException {
        Request request = new Request.Builder().url("https://bs.smallway.top/downloadlist?hash=" + hash).build();

        Call call = httpClient.newCall(request);
        Response response = call.execute();
        String static_ = response.body().string();
        return true;
    }

    public static boolean SendMail(String subject, String body) throws IOException {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/mail").newBuilder();
        urlBuilder.addQueryParameter("subject", subject);
        urlBuilder.addQueryParameter("body", body);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();

        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return true;

    }

    public static String GetplayInfo(String nickname, String code) throws IOException {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("http://gateway.lightband.cn:3005/activity/api/activity/playInfo").newBuilder();
        urlBuilder.addQueryParameter("nickname", nickname);
        urlBuilder.addQueryParameter("code", code);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static void submitid(String id) throws IOException {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/submitID").newBuilder();
        urlBuilder.addQueryParameter("id", id);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        Response response = call.execute();
    }

    public static String gettoken() {
        Request request = new Request.Builder().url("https://bs.smallway.top/gettoken/").build();
        Call call = httpClient.newCall(request);
        Response response = null;
        String token = null;
        try {
            response = call.execute();
            token = JSONObject.parseObject(response.body().string()).getString("token");
        } catch (IOException e) {
            return "";
        }
        return token;
    }

    public static String uploadkey(String id, String key, String hash, String name) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/upload/").newBuilder();
        urlBuilder.addQueryParameter("id", id);
        urlBuilder.addQueryParameter("key", key);
        urlBuilder.addQueryParameter("hash", hash);
        urlBuilder.addQueryParameter("name", name);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                return "收藏成功";
            } else {
                return response.body().string();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static JSONObject getScorelist(String code) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder().url("http://gateway.lightband.cn:3005/activity/api/activity/playInfo?code=" + code + "&nickname=").get().build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return JSONObject.parseObject(response.body().string());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static List<Keys> GetStartListKey(String id) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/getkey/").newBuilder();
        urlBuilder.addQueryParameter("id", id);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        List<Keys> keys = new ArrayList<>();
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                JSONArray jsonArray = JSONArray.parseArray(response.body().string());
                for (int i = 0; i < jsonArray.size(); i++) {
                    String key = jsonArray.getJSONObject(i).getJSONObject("fields").getString("key");
                    String hash = jsonArray.getJSONObject(i).getJSONObject("fields").getString("hash");
                    String name = jsonArray.getJSONObject(i).getJSONObject("fields").getString("name");
                    String pk = jsonArray.getJSONObject(i).getString("pk");
                    keys.add(new Keys(key, hash, name, pk));
                }
                return keys;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return keys;
    }

    public static String DelStartListKey(String pk) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/delkey/").newBuilder();
        urlBuilder.addQueryParameter("id", pk);
        String url = urlBuilder.build().toString();
        Request request = new Request.Builder().url(url).build();
        Call call = httpClient.newCall(request);
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (IOException e) {
            return "删除收藏失败";
        }
        return null;
    }

    public static void SearchData(String data, String minNpsStr, String maxNpsStr) {
        // HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/search/").newBuilder();
//        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/search/").newBuilder();
//        urlBuilder.addQueryParameter("data", data);
        String url = "https://beatsaver.com/api/search/text/0";

        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        urlBuilder.addQueryParameter("q", data);
        urlBuilder.addQueryParameter("minNps", minNpsStr);
        urlBuilder.addQueryParameter("maxNps", maxNpsStr);
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        try {
            Response response = call.execute();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static com.alibaba.fastjson.JSONObject GetVersion() {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/getversion/").newBuilder();
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        try {
            Response response = call.execute();
            return com.alibaba.fastjson.JSONObject.parseObject(response.body().string());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static com.alibaba.fastjson2.JSONArray GetWords() {
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://bs.smallway.top/get_word/").newBuilder();
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        try {
            Response response = call.execute();

            com.alibaba.fastjson2.JSONArray jsonArray = JSONObject.parseObject(response.body().string()).getJSONArray("data");

            return jsonArray;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static com.alibaba.fastjson.JSONObject getHash(String url) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                return com.alibaba.fastjson.JSONObject.parseObject(response.body().string());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static JSONObject GetSongByAuthorId(String id) {
        String url = "https://beatsaver.com/api/maps/collaborations/" + id;
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                return JSONObject.parseObject(response.body().string());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

